package com.auto.learning.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout.LayoutParams;

import com.amap.api.location.c;
import com.auto.learning.R;
import com.auto.learning.bean.ImagesBean;
import com.auto.learning.event.Handle;
import com.auto.learning.tools.CommonMethod;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

public class CommentImageAdapter extends BaseAdapter {
	private Context context;
	private ArrayList<ImagesBean> imageList;
	private Handle handle;

	public CommentImageAdapter(Context context,ArrayList<ImagesBean> imageList,Handle handle) {
		this.context = context;
		this.imageList = imageList;
		this.handle = handle;
	}

	@Override
	public int getCount() {
		return imageList.size();
	}

	@Override
	public Object getItem(int position) {
		return imageList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return imageList.get(position).getImgId();
	}

	ViewHolder holder = null;
	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = LayoutInflater.from(context).inflate(R.layout.comment_image_adapter, null);
			holder.image_item = (ImageView) convertView.findViewById(R.id.image_item);
			convertView.setTag(holder);
		}else {
			holder = (ViewHolder) convertView.getTag();
		}
		LayoutParams layoutParams = (LayoutParams) holder.image_item.getLayoutParams();
		if (imageList.size() == 1) {
			layoutParams.width = CommonMethod.dip2px(context, 130);
			layoutParams.height = CommonMethod.dip2px(context, 180);
		}else {
			layoutParams.width = CommonMethod.dip2px(context, 100);
			layoutParams.height = CommonMethod.dip2px(context, 100);
		}
		holder.image_item.setLayoutParams(layoutParams);
		ImageLoader.getInstance().displayImage(imageList.get(position).getPreviewUrl(),holder.image_item);
		holder.image_item.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				handle.obtainMessage(10, position, 0, imageList).sendToTarget();
			}
		});
		return convertView;
	}
	
	class ViewHolder{
		ImageView image_item;
	}
}
